﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Web.UI.WebControls;
namespace TwentyTree4Umbraco.Controls
{
    public class VideoPicker : Panel
    {
        private HiddenField txtValue;

        protected override void OnInit(EventArgs e)
        {
             txtValue = new HiddenField();
             this.Controls.Add(txtValue);
             base.OnInit(e);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            Data d = new Data();
            XmlNode photos = d.GetPhotos();

            string photoHtml = string.Empty;


            if (this.Value != string.Empty)
            {
                XmlNode selected = photos.SelectSingleNode(string.Format("//photos [photo_id = '{0}']", this.Value));

                string selectedtitle = selected.SelectSingleNode("./title").FirstChild.Value;


                photoHtml = String.Format(
                    "<div id=\"videoChosen{0}\"><span id=\"videoName{0}\">{1}</span> chosen  <br/> <a href=\"#\" onclick=\"jQuery('#videoList{0}').show(); jQuery('#videoChosen{0}').hide();\">Chose another</a> <a style=\"color:red\" href=\"#\" onclick=\"document.getElementById('{0}').value = '';jQuery('#videoList{0}').show(); jQuery('#videoChosen{0}').hide();\">Remove</a></div><table id=\"videoList{0}\" border=\"0\" cellpadding=\"5\" style=\"display:none\">",
                    txtValue.ClientID,
                    selectedtitle);
            }
            else
            {

                photoHtml = String.Format(
                    "<div id=\"videoChosen{0}\" style=\"display: none;\"><span id=\"videoName{0}\"></span> chosen  <br/> <a href=\"#\" onclick=\"jQuery('#videoList{0}').show(); jQuery('#videoChosen{0}').hide();\">Chose another</a> <a style=\"color:red\" href=\"#\" onclick=\"document.getElementById('{0}').value = '';jQuery('#videoList{0}').show(); jQuery('#videoChosen{0}').hide();\">Remove</a></div><table id=\"videoList{0}\" border=\"0\" cellpadding=\"5\">",
                    txtValue.ClientID);
            }

            foreach (XmlNode photo in photos.SelectNodes("//photos"))
            {

                string title = photo.SelectSingleNode("./title").FirstChild.Value;
                string author = photo.SelectSingleNode("./display_name").FirstChild.Value;
                string thumbnail = photo.SelectSingleNode("./quad100_download").FirstChild.Value;
                string date = photo.SelectSingleNode("./creation_date__date").FirstChild.Value;
                string photoId = photo.SelectSingleNode("./photo_id").FirstChild.Value;

                photoHtml += String.Format("<tr><td valign=\"top\"><a href=\"#\" onclick=\"document.getElementById('{4}').value = '{5}'; jQuery('#videoName{4}').text('{6}'); jQuery('#videoList{4}').hide(); jQuery('#videoChosen{4}').show();\"><img src=\"{0}\" style=\"border: none;\" /></a></td><td valign=\"top\"><h3>{1}</h3><p>By {2} @ {3}</p></td></tr>",
                d.Url + thumbnail,
                title,
                author,
                date,
                txtValue.ClientID,
                photoId,
                title.Replace("'", "\\'")
                );

            }
            writer.WriteLine(photoHtml + "</table>");


            base.Render(writer);
        
        }

        public string Value
        {

            get
            {
                return txtValue.Value;
            }

            set
            {
                txtValue.Value = value;
            }
        }
    }
}
